package com.example.test

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView

/**
 * 仿ViewPager实现抖音滚动的RecyclerView的布局
 * 竖直方向监听竖直位移量，水平方向监听水平位移量
 */
class PagerLinearLayoutManager : LinearLayoutManager {

    companion object{
        const val TAG = "PagerLinearLayoutManager"
    }

    private var pagerSnapHelper: PagerSnapHelper = PagerSnapHelper()
    private var onPagerListener: OnPagerListener? = null
    private lateinit var recyclerView: RecyclerView
    private var moveX = 0
    private var moveY = 0

    constructor(context: Context) : this(context, OrientationHelper.VERTICAL)

    constructor(context: Context, orientation: Int) : this(context, orientation, false)

    constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(
            context,
            orientation,
            reverseLayout
    )

    override fun onAttachedToWindow(view: RecyclerView) {
        super.onAttachedToWindow(view)
        pagerSnapHelper.attachToRecyclerView(view) //设置RecyclerView每次只能滚动一页
        recyclerView = view
        recyclerView.addOnChildAttachStateChangeListener(object :
                RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewAttachedToWindow(view: View) {
                if (onPagerListener != null && childCount == 1) {
                    onPagerListener!!.onInitComplete(getPosition(view))
                }
            }

            override fun onChildViewDetachedFromWindow(view: View) {
                when (orientation) {
                    OrientationHelper.VERTICAL -> {
                        if (moveY >= 0) {
                            onPagerListener?.onPageRelease(true, getPosition(view))
                        }else{
                            onPagerListener?.onPageRelease(false, getPosition(view))
                        }
                    }
                    OrientationHelper.HORIZONTAL -> {
                        if (moveX >= 0) {
                            onPagerListener?.onPageRelease(true, getPosition(view))
                        }else{
                            onPagerListener?.onPageRelease(false, getPosition(view))
                        }
                    }
                }
            }

        })
    }

    /**
     * 滑动状态的改变
     * 缓慢拖拽-> SCROLL_STATE_DRAGGING
     * 快速滚动-> SCROLL_STATE_SETTLING
     * 空闲状态-> SCROLL_STATE_IDLE
     */
    override fun onScrollStateChanged(state: Int) {
        when (state) {
            RecyclerView.SCROLL_STATE_IDLE -> {
                val viewIdle = pagerSnapHelper.findSnapView(this)
                val positionIdle = getPosition(viewIdle!!)
                if (onPagerListener != null && childCount == 1) {
                    onPagerListener!!.onPageSelected(
                            positionIdle,
                            positionIdle == itemCount - 1
                    )
                }
            }
            RecyclerView.SCROLL_STATE_DRAGGING -> {
                val viewDrag = pagerSnapHelper.findSnapView(this)
                val positionDrag = getPosition(viewDrag!!)
            }
            RecyclerView.SCROLL_STATE_SETTLING -> {
                val viewSetting = pagerSnapHelper.findSnapView(this)
                val positionSetting = getPosition(viewSetting!!)
            }
        }
    }

    /**
     * 监听竖直方向的相对偏移量
     */
    override fun scrollVerticallyBy(
            dy: Int,
            recycler: RecyclerView.Recycler?,
            state: RecyclerView.State?
    ): Int {
        moveY = dy
        return super.scrollVerticallyBy(dy, recycler, state)
    }

    /**
     * 监听水平方向的相对偏移量
     */
    override fun scrollHorizontallyBy(
            dx: Int,
            recycler: RecyclerView.Recycler?,
            state: RecyclerView.State?
    ): Int {
        moveX = dx
        return super.scrollHorizontallyBy(dx, recycler, state)
    }

    fun setOnPagerListener(pagerListener: OnPagerListener) {
        this.onPagerListener = pagerListener
    }

    interface OnPagerListener {
        /*监听初始化完成*/
        fun onInitComplete(position: Int)

        /*监听释放*/
        fun onPageRelease(isNext: Boolean, position: Int)

        /*监听选中以及判断是否滑动到底部*/
        fun onPageSelected(position: Int, isBottom: Boolean)
    }

}